package com.java.java基础.线程;

import java.util.Locale;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 * User: huangxuelei
 * Time: 16:08
 * 1.在main方法中启动2个线程
 * 2.第一个线程打印1-100
 * 3.知道第2个线程从键盘读取“Q”命名
 */
public class Thread6HomeWork {
    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        a.start();
        b.start();
    }
}

class A extends Thread{
    private boolean loop = true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
    @Override
    public void run() {
       while(loop){
           System.out.println("输出1-100的数字");
           int v = (int)(Math.random() * 100 + 1);
           System.out.println(Thread.currentThread().getName() + ":" + v);
           try {
               Thread.sleep(1000);
           }catch (InterruptedException e){
               e.printStackTrace();
           }
       }
    }
}

class B extends Thread{
    private A a;
    private Scanner scanner =new Scanner(System.in);
    public B(A a){ // 构造方法 传入A对象
        this.a = a;
    }
    @Override
    public void run() {
        while (true){
            System.out.println("输入Q退出");
            char Key = scanner.next().toUpperCase().charAt(0);
            System.out.println("输入的字符是：" + Key);
            if (Key == 'Q'){
                //以通知的方式结束01线程
                this.a.setLoop(false);
                System.out.println("退出");
                break;
            }
        }
    }
}
